<template>
  <div id="areaCodeAdd" class="same-change-oldstyle">
    <div class="same-head-box" style="border-bottom:0px">
      <div class="saem-seat-box">
        <div class="saem-seat-left">
          <span>当前位置：</span>
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>园区管理</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: '/areaCodeList' }">区域码管理</el-breadcrumb-item>
            <el-breadcrumb-item>添加区域码</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="saem-seat-right">
          <!-- <button class="same-search-btns same-search-export" @click="RefreshFun">刷新</button> -->
          <button class="same-search-btns" @click="returnBack">返回</button>
        </div>
      </div>
    </div>
    <div class="editwrap">
      <h4 class="four">区域码信息</h4>
      <el-row type="flex" class="m-10">
        <div class="formitem">
          <span class="title">区域码名称</span>
          <el-input type="text" name="name" v-model="name" placeholder="请输入区域码名称 (如:幸福小区区域码)" style="width:360px" />
          <i class="icon">*</i>
        </div>
      </el-row>
      <el-row type="flex" class="m-10">
        <div class="formitem">
          <span class="title">A秘钥</span>
          <el-select v-model="keyAIndex" placeholder="请选择A秘钥" style="width:360px" @change="handleKeyA">
            <el-option v-for="(item,index) in arrlistA" :key="item.name" :label="item.name" :value="index"></el-option>
          </el-select>
          <i class="icon">*</i>
        </div>
      </el-row>
      <el-row type="flex" class="m-10">
        <div class="formitem">
          <span class="title">B秘钥</span>
          <el-select v-model="keyBIndex" placeholder="请选择B秘钥" style="width:360px" @change="handleKeyB">
            <el-option v-for="(item,index) in arrlistB" :key="item.name" :label="item.name" :value="index"></el-option>
          </el-select>
          <i class="icon"></i>
        </div>
      </el-row>
      <el-row type="flex" class="m-10">
        <div class="formitem">
          <span class="title">区域码</span>
          <div class="onlyOne" v-for="(item,index) in inputList" :key="index">
            <input type="text" name="name" maxlength="1" v-model="item.val" @keyup="nextFocus($event,index)"
              @keydown="changeValue(index)" @input="changeInput(item.val,index)" class="border-input"
              style="width:50px;margin-right:9px" autocomplete="off" />
          </div>
          <i class="icon">*</i>
          <p class="hint_icon">必须是6位数，数字0-9和字母A-F的组合,不区分大小写，不填系统将自动生成</p>

        </div>
      </el-row>

      <el-row type="flex" class="m-10" style="padding-top:20px">
        <div class="formitem">
          <span class="title">存储位置</span>
          <el-radio-group v-model="storage_location" text-color="#2C5586" fill="#2C5586">
            <el-radio :label="0">不校验区域码</el-radio>
            <el-radio :label="1">位置一</el-radio>
            <el-radio :label="2">位置二</el-radio>
            <el-radio :label="3">位置三</el-radio>
          </el-radio-group>
          <i class="icon">*</i>
        </div>
      </el-row>

      <el-row type="flex" class="m-10" style="padding-top:20px">
        <div class="formitem">
          <span class="title">状态</span>
          <!-- <el-switch v-model="is_frozen" active-text="正常" :active-value="1" inactive-text="冻结" :inactive-value="-1"
              active-color="#2c5586" inactive-color="#cccccc" ></el-switch> -->
          <el-radio-group v-model="is_frozen" text-color="#2C5586" fill="#2C5586">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="-1">冻结</el-radio>
          </el-radio-group>
          <i class="icon">*</i>
        </div>
      </el-row>

      <!-- <div class="areaCodeAddtip">区域号码根据名称直接配置</div>
      <el-row type="flex" class="m-10">
        <div class="items">
          <div class="itemsbot">
            <span class="lable" style="margin-right: 10px">状态</span>
            <el-select v-model="is_frozen" placeholder="请选择区域状态" @change="changeState">
              <el-option v-for="item in stateList" :key="item.stateId" :label="item.stateName" :value="item.stateId"></el-option>
            </el-select>
            <el-select size="medium" v-model="state" placeholder="请选择" style="width: 140px;">
							<el-option v-for="item in stateoption" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
                       <i class="icon" style="margin-left:1px !important">*</i>
          </div>
        </div>
      </el-row> -->
      <el-button size="medium" type="primary" @click="addAreaCode">提 交</el-button>
      <!-- <button class="binding" @click="addAreaCode">提交</button> -->
    </div>
  </div>
</template>

<script>
  var is_commit = true;
  import $ from 'jquery';
  export default {
    name: "areaCodeAdd",
    data: function () {
      return {
        name: "", //名称
        is_frozen: 1,
        area_code: '',
        inputList: [{ val: "0" }, { val: "0" }, { val: "" }, { val: "" }, { val: "" }, { val: "" }],
        stateList: [
          {
            stateId: 1,
            stateName: "正常"
          },
          {
            stateId: -1,
            stateName: "冻结"
          }
        ],
        arrlistA: [],
        keyA: '',
        keyAIndex: '',
        arrlistB: [],
        keyB: '',
        keyBIndex: '',
        areacodes_id: '',//当前新增的园区id
        storage_location : 1,
        is_areacode: this.$route.query.is_areacode == 1 ? true : false,//是否是新增页面进来
      };
    },
    inject: [
      'reload'
    ],
    //进入之前
    beforeRouteEnter(to, from, next) {
      next();
    },
    //离开之前 将新增的id返回到路由中 一个新增一个编辑
    beforeRouteLeave(to, from, next) {
      if (to.path == '/management' || to.path == '/areacode') {
        to.meta.areacodes_id = this.areacodes_id;
      }
      next();
    },
    mounted() { },
    created() {
      this.getCardMotherKeyConfig();
    },
    methods: {
      handleKeyA(e) {
        this.keyAIndex = e;
        this.keyA = this.arrlistA[e].value
      },

      handleKeyB(e) {
        this.keyBIndex = e;
        this.keyB = this.arrlistB[e].value
      },

      changeInput(val, idx) {
        // this.$message.closeAll();
        // let gettype = val.toString().replace(/[^A-Fa-f0-9]/g, '');
        // console.log('>>>>',val,gettype,!gettype);
        // if(gettype==''&&val!=""){
        //      this.$message.error('只能是0-9A-F的字母');
        //      this.inputList[idx].val = "";
        //      return false;
        // }
      },
      /*对焦到下一个input框去*/
      nextFocus(el, index) {
        this.$message.closeAll();
        let gettype = el.key.toString().replace(/[^A-Fa-f0-9]/g, '');
        // console.log('聚焦',el.key,index,gettype);
        if (gettype == '' && el.key != "") {
          this.$message.error('区域码必须是6位且为0-9的数字和a-f的字母组合');
          this.inputList[index].val = "";
          return false;
        }
        var dom = document.getElementsByClassName("border-input"),
          currInput = dom[index],
          nextInput = dom[index + 1],
          lastInput = dom[index - 1];
        /*这里的keyCode 根据不同的平台或许不同,安卓就是不是8*/
        if (el.keyCode != 8) {
          if (index < (this.inputList.length - 1)) {
            nextInput.focus();
          } else {
            currInput.blur();
          }
        } else {
          if (index != 0) {
            lastInput.focus();
          }
        }
        var getList = this.inputList;
        var contCat = '';
        getList.forEach((item, index) => {
          if (item.val) {
            contCat += item.val;
          }
        })
        this.area_code = contCat;
        // console.log('contCat',contCat)
      },
      /*当键盘按下的时候清空原有的数*/
      changeValue(index) {
        // this.inputList[index].val = "";
      },
      RefreshFun() {
        // 调用reload方法，刷新整个页面
        this.reload()
      },
      //返回
      returnBack() {
        this.$router.back(-1);
      },
      changeState(e) {
        this.is_frozen = e;
      },
      //获取A秘钥和B秘钥
      getCardMotherKeyConfig() {
        var that = this;
        // Ajax请求  type:1通用 2兼容
        that.$post({
          url: "Common/Common/getCardMotherKeyConfig",
          data: {
            type: 1
          },
          success: data => {
            console.log('得到的数据', data);
            if (data.code == 1000) {
              that.arrlistA = data.data.key;
              that.arrlistB = data.data.key_b;
              // 赋默认值
              if (data.data.key) {
                that.keyA = data.data.key[0].value;
                that.keyAIndex = 0;
              }
              if (data.data.key_b) {
                that.keyB = data.data.key_b[0].value;
                that.keyBIndex = 0
              }
            } else {
              that.$message.error(data.message);
            }
          }
        });


      },
      //添加区域码
      addAreaCode() {
        var that = this;
        var name = that.name;
        var keyA = that.keyA;
        var keyB = that.keyB;
        var area_code = that.area_code == '00' ? '' : that.area_code;
        that.$message.closeAll();
        if (name == '') {
          that.$message.error('区域码名称不能为空'); return false;
        }
        if (keyA == '') {
          that.$message.error('A秘钥不能为空'); return false;
        }
        if (area_code.length <= 5 && area_code.length >= 3) {
          that.$message.error('区域码必须是6位'); return false;
        }
        console.log('数据额额度', name, keyA, keyB, area_code);
        if (!is_commit) {
          return false;
        }
        is_commit = false;
        // Ajax请求
        that.$post({
          url: "Partner/CardMother/addCardMother",
          data: {
            name: name,
            key: keyA,
            key_b: keyB,
            area_code: area_code,
            is_frozen: that.is_frozen,
            storage_location:that.storage_location
          },
          success: data => {
            if (data.code == 1000) {
              that.$message.success(data.message);
              //是否是快捷添加
              if (this.is_areacode) {
                this.areacodes_id = data.data.id;
                this.$router.go(-1);
              } else {
                this.$router.push({ path: "/areaCodeList" });
              }
            } else {
              that.$message.error(data.message);
            }
            is_commit = true;
          }
        });
      }
    }
  };
</script>

<style>
  .block {
    margin-left: 10px !important;
  }

  #areaCodeAdd .check_record_card_time .el-input--suffix .el-input__inner {
    padding-left: 15px !important;
  }

  #areaCodeAdd .check_record_card_time .check_record_card_time_box .el-input--suffix .el-input__inner {
    padding-left: 30px !important;
  }

  #areaCodeAdd {
    background-color: #fff;
  }

  #areaCodeAdd .editwrap {
    padding: 10px 40px 30px;
  }

  #areaCodeAdd .formitem {
    display: flex;
    align-items: center;
    margin: 10px 0;
  }

  #areaCodeAdd .formitem input {
    /* width: 286px; */
    height: 38px;
    border: 1px solid #cccccc;
    border-radius: 4px;
    padding: 0 15px;
  }


  #areaCodeAdd .selectitem {
    margin-top: 40px;
  }

  #areaCodeAdd .selectitem input {
    width: 246px;
  }

  #areaCodeAdd .inputOne .el-input>input {
    width: 100px;
  }

  #areaCodeAdd .addressitem {
    /* width: 360px; */
    display: inline-flex;
  }

  #areaCodeAdd .items {
    margin-top: 10px;
  }

  #areaCodeAdd .el-input--suffix .el-input__inner {
    height: 38px;
  }

  #areaCodeAdd .title {
    font-size: 16px;
    color: #323333;
    padding-right: 15px;
    min-width: 80px;
  }

  #areaCodeAdd .icon {
    /*font-size: 28px;*/
    /*color: #bc1b21;*/
    /*color: #9e383b;*/
    /*margin-left: 13px;*/
    font-style: normal;
    margin-left: 5px;
    color: #FF6565;
  }

  #areaCodeAdd .hint_icon {
    position: absolute;
    bottom: -10px;
    left: 96px;
    font-size: 12px;
    font-weight: 400;
    color: #B3B3B3;
    opacity: 0.8;
  }

  #areaCodeAdd .areaCodeAddtip {
    margin-left: 47px;
    margin-top: -7px;
    color: #FF6565;
    font-size: 14px;
  }

  #areaCodeAdd .check_record_card_time .icon {
    font-style: normal;
    margin-left: 5px;
    color: #FF6565;
  }

  #areaCodeAdd table {
    width: 400px;
    height: auto;
    margin-top: 10px;
    border: 1px solid #dde8f6;
    background-color: #f5f5f5;
    padding: 10px 0;
  }

  #areaCodeAdd table td {
    padding: 10px 0 10px 20px;
    font-size: 16px;
    color: #323333;
  }

  #areaCodeAdd .tpl_W-show td:nth-child(1) {
    width: 100px;
  }

  #areaCodeAdd .tpl_W-show td:nth-child(2) {
    width: 160px;
  }

  #areaCodeAdd .tpl_W-show td:nth-child(3) {
    width: 140px;
  }

  #areaCodeAdd .binding {
    outline: none;
    border: 0;
    width: 80px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #fff;
    background-color: #2c5586;
    font-size: 16px;
    font-weight: bold;
    border-radius: 3px;
    margin-top: 20px;
  }

  #areaCodeAdd .el-radio__input.is-checked .el-radio__inner {
    border-color: #2c5586;
    background: #2c5586;
  }

  #areaCodeAdd .el-radio__input.is-checked+.el-radio__label {
    color: #2c5586;
  }

  #areaCodeAdd .mapwrap {
    display: flex;
  }

  #areaCodeAdd #allmap {
    width: 80%;
    height: 500px;
  }

  #areaCodeAdd .leftlist {
    width: 20%;
  }

  #areaCodeAdd .leftlist .equipmentitem {
    font-size: 14px;
    cursor: pointer;
    margin: 10px 0;
  }

  #areaCodeAdd .leftlist .equipmentname {
    display: flex;
    align-items: center;
  }

  #deviceadd .leftlist .equipmentname img {
    width: 18px;
    display: block;
  }

  #areaCodeAdd .leftlist .equipmentname .dingwei {
    position: relative;
  }

  #areaCodeAdd .leftlist p {
    padding-left: 28px;
    padding-top: 5px;
  }

  #areaCodeAdd .leftlist .equipmentname .dingwei span {
    color: #fff;
    font-weight: bold;
    position: absolute;
    text-align: center;
    width: 100%;
    top: 2px;
    font-size: 12px;
  }

  .m-10 {
    margin: 10px 0;
  }

  #areaCodeAdd .formitem .onlyOne {
    position: relative;
  }

  #areaCodeAdd .formitem .onlyOne input {
    padding: 0px;
    text-align: center;
  }
</style>
